#include "tinyxml/tinyxml.h"
#include "XMLParser.h"

map<double,double> parseSVCHImpulseVoltageArray(string svchImpulseVoltageChar)
{
	map<double,double> result;
	TiXmlDocument Document;
	Document.Parse(svchImpulseVoltageChar.c_str(), 0, TIXML_ENCODING_UTF8);
	TiXmlElement* rootElement = Document.FirstChildElement("out");

	int count= 0;
	for (TiXmlElement* entryElem = rootElement->FirstChildElement("entry");entryElem;entryElem = entryElem->NextSiblingElement( "entry" ))
	{
		TiXmlElement* timeElem = entryElem->FirstChildElement("inputTime");
		TiXmlElement* voltageElem = entryElem->FirstChildElement("outputVoltage");
		result[atof(timeElem->GetText())] = atof(voltageElem->GetText());

	};	

	return result;

}


shared_ptr<std::vector<TractDTO*>> parseTractDTOXml(string tractDTOXml)
{
	shared_ptr<vector<TractDTO*>> tractsVector(new vector<TractDTO*>);

	TiXmlDocument Document;
	Document.Parse(tractDTOXml.c_str(), 0, TIXML_ENCODING_UTF8);
	TiXmlElement* rootElement = Document.FirstChildElement("data");

	for (TiXmlElement* tractElem = rootElement->FirstChildElement("tract");tractElem;tractElem = tractElem->NextSiblingElement( "tract" ))
	{

		TiXmlElement* approximationCoefficient_K = tractElem->FirstChildElement("approximationCoefficient_K");
		TiXmlElement* approximationCoefficient_N = tractElem->FirstChildElement("approximationCoefficient_N");
		TiXmlElement* voltage_U0 = tractElem->FirstChildElement("voltage_U0");
		TiXmlElement* stepness_S = tractElem->FirstChildElement("stepness_S");

		double approximationCoefficient_Kval =  atof(approximationCoefficient_K->GetText());
		double approximationCoefficient_Nval = atof(approximationCoefficient_N->GetText());
		double voltage_U0val = atof(voltage_U0->GetText());
		double stepness_Sval = atof(stepness_S->GetText());
		TractDTO* tempTractDTO = new TractDTO(approximationCoefficient_Kval,approximationCoefficient_Nval,voltage_U0val,stepness_Sval); 
		tractsVector->push_back(tempTractDTO);
	};
	return tractsVector;

}


shared_ptr<SVCHImpulseDTO> parseSvchDTOXml(string svchDTOXml)
{
	shared_ptr<SVCHImpulseDTO> svchImpulseDTO;

	TiXmlDocument Document;
	Document.Parse(svchDTOXml.c_str(), 0, TIXML_ENCODING_UTF8);
	TiXmlElement* rootElement = Document.FirstChildElement("data");

	for (TiXmlElement* entryElem = rootElement->FirstChildElement("entry");entryElem;entryElem = entryElem->NextSiblingElement("entry"))
	{
		TiXmlElement* impulseIncreaseConstant_Alpha = entryElem->FirstChildElement("impulseIncreaseConstant_Alpha");
		TiXmlElement* impulseFallingConstant_Beta = entryElem->FirstChildElement("impulseFallingConstant_Beta");
		TiXmlElement* voltageAplitude_A = entryElem->FirstChildElement("voltageAplitude_A");
		TiXmlElement* impulseFillingFrequency_U0 = entryElem->FirstChildElement("impulseFillingFrequency_U0");

		double impulseIncreaseConstant_Alpha_val =  atof(impulseIncreaseConstant_Alpha->GetText());
		double impulseFallingConstant_Beta_val = atof(impulseFallingConstant_Beta->GetText());
		double voltageAplitude_A_val = atof(voltageAplitude_A->GetText());
		double impulseFillingFrequency_U0_val = atof(impulseFillingFrequency_U0->GetText());
		SVCHImpulseDTO* dto =new SVCHImpulseDTO(impulseIncreaseConstant_Alpha_val,impulseFallingConstant_Beta_val,voltageAplitude_A_val,impulseFillingFrequency_U0_val);
		svchImpulseDTO = shared_ptr<SVCHImpulseDTO>(dto); 

	};
	return svchImpulseDTO;

}


//TODO write 
TractSVCHReactionInputDTO* parseTractSVCHReactionInputXML(string tractSVCHReactionInputXML)
{
	vector<TractDTO*> tractVector;
	map<double,double> svchMap;

	TiXmlDocument Document;
	Document.Parse(tractSVCHReactionInputXML.c_str(), 0, TIXML_ENCODING_UTF8);
	TiXmlElement* rootElement = Document.FirstChildElement("data");
	for (TiXmlElement* tractElem = rootElement->FirstChildElement("tract");tractElem;tractElem = tractElem->NextSiblingElement( "tract" ))
	{

		TiXmlElement* approximationCoefficient_K = tractElem->FirstChildElement("approximationCoefficient_K");
		TiXmlElement* approximationCoefficient_N = tractElem->FirstChildElement("approximationCoefficient_N");
		TiXmlElement* voltage_U0 = tractElem->FirstChildElement("voltage_U0");
		TiXmlElement* stepness_S = tractElem->FirstChildElement("stepness_S");

		double approximationCoefficient_Kval =  atof(approximationCoefficient_K->GetText());
		double approximationCoefficient_Nval = atof(approximationCoefficient_N->GetText());
		double voltage_U0val = atof(voltage_U0->GetText());
		double stepness_Sval = atof(stepness_S->GetText());
		TractDTO* tempTractDTO = new TractDTO(approximationCoefficient_Kval,approximationCoefficient_Nval,voltage_U0val,stepness_Sval); 
		tractVector.push_back(tempTractDTO);
	};
	TiXmlElement* svchImpulseVoltageArrayElem  = rootElement->FirstChildElement("svchImpulseVoltageArray");

	for (TiXmlElement* entryElem = svchImpulseVoltageArrayElem->FirstChildElement("entry");entryElem;entryElem = entryElem->NextSiblingElement("entry"))
	{

		TiXmlElement* inputTimeElem = entryElem->FirstChildElement("inputTime");
		TiXmlElement* outputVoltageElem = entryElem->FirstChildElement("outputVoltage");

		double inputTime =  atof(inputTimeElem->GetText());
		double outputVoltage = atof(outputVoltageElem->GetText());
		svchMap[inputTime] =outputVoltage;

	};
	TractSVCHReactionInputDTO* tractSVCHReactionInputDTO = new TractSVCHReactionInputDTO(tractVector,svchMap);
	return tractSVCHReactionInputDTO;
}